home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume27 / regexpr / part01 next >
Encoding:
Text File  |  1992-01-03  |  50.8 KB  |  1,891 lines

  1. Newsgroups: comp.sources.misc
  2. From: ylo@ngs.fi (Tatu Ylonen)
  3. Subject: v27i023:  regexpr - regexp library compatible with gnu regex, Part01/01
  4. Message-ID: <1992Jan3.031320.2360@sparky.imd.sterling.com>
  5. X-Md4-Signature: 81e4c7ebca59ef67b5925ed8e7ecb2a4
  6. Date: Fri, 3 Jan 1992 03:13:20 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: ylo@ngs.fi (Tatu Ylonen)
  10. Posting-number: Volume 27, Issue 23
  11. Archive-name: regexpr/part01
  12. Environment: UNIX, MSDOS
  13.  
  14. Regexpr is a regular expression package.  It is free (meaning that you
  15. may do anything you want with it); the original motivation for writing
  16. it was not being able to use the GNU library in a commercial
  17. application.
  18.  
  19. Some of the features include:
  20.   - fully compatible with gnu regex library (I run emacs with this
  21.     library for several weeks as a test)
  22.   - can handle arbitrary data, including binary characters
  23.   - can handle split data
  24.   - compiles and runs also on 16 bit machines (eg. MSDOS)
  25.   - does not use alloca
  26.   - fairly easy to extend and modify (easier than the gnu version anyway)
  27.   - speed comparable to that of the GNU library (searches seem a bit
  28.     faster, matches about the same and compiling a bit slower than in
  29.     the gnu library)
  30.   - there are some extensions (enabled if RE_ANSI_HEX is set in syntax):
  31.       \vnn for accessing registers > 9 (useful if RE_NREGS > 10)
  32.       \xhh specifies character in hex
  33.       \a   ascii 7
  34.       \b   ascii 8
  35.       \f   ascii 12
  36.       \n   ascii 10
  37.       \r   ascii 13
  38.       \t   ascii 9
  39.       \v   ascii 11
  40.  
  41. I have not written any documentation; see the header file and
  42. documentation GNU Regex library in GNU Emacs distribution.
  43.  
  44. Send comments, bug fixes and suggestions to Tatu Ylonen
  45. <ylo@cs.hut.fi>.
  46.  
  47. #! /bin/sh
  48. # This is a shell archive.  Remove anything before this line, then unpack
  49. # it by saving it into a file and typing "sh file".  To overwrite existing
  50. # files, type "sh file -c".  You can also feed this as standard input via
  51. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  52. # will see the following message at the end:
  53. #        "End of shell archive."
  54. # Contents:  regexpr.h regexpr.c
  55. # Wrapped by ylo@ngs.fi on Mon Dec 30 09:18:58 1991
  56. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  57. if test -f regexpr.h -a "${1}" != "-c" ; then 
  58.   echo shar: Will not over-write existing file \"regexpr.h\"
  59. else
  60. echo shar: Extracting \"regexpr.h\" \(5275 characters\)
  61. sed "s/^X//" >regexpr.h <<'END_OF_regexpr.h'
  62. X/*
  63. X
  64. Xregexpr.h
  65. X
  66. XAuthor: Tatu Ylonen <ylo@ngs.fi>
  67. X
  68. XCopyright (c) 1991 Tatu Ylonen, Espoo, Finland
  69. X
  70. XPermission to use, copy, modify, distribute, and sell this software
  71. Xand its documentation for any purpose is hereby granted without fee,
  72. Xprovided that the above copyright notice appear in all copies.  This
  73. Xsoftware is provided "as is" without express or implied warranty.
  74. X
  75. XCreated: Thu Sep 26 17:15:36 1991 ylo
  76. XLast modified: Mon Nov  4 15:49:46 1991 ylo
  77. X
  78. X*/
  79. X
  80. X#ifndef REGEXPR_H
  81. X#define REGEXPR_H
  82. X
  83. X#define RE_NREGS    10  /* number of registers available */
  84. X
  85. Xtypedef struct re_pattern_buffer
  86. X{
  87. X  char *buffer;      /* compiled pattern */
  88. X  int allocated;     /* allocated size of compiled pattern */
  89. X  int used;         /* actual length of compiled pattern */
  90. X  char *fastmap;     /* fastmap[ch] is true if ch can start pattern */
  91. X  char *translate;     /* translation to apply during compilation/matching */
  92. X  char fastmap_accurate; /* true if fastmap is valid */
  93. X  char can_be_null;     /* true if can match empty string */
  94. X  char uses_registers;     /* registers are used and need to be initialized */
  95. X  char anchor;         /* anchor: 0=none 1=begline 2=begbuf */
  96. X} *regexp_t;
  97. X
  98. Xtypedef struct re_registers
  99. X{
  100. X  int start[RE_NREGS];  /* start offset of region */
  101. X  int end[RE_NREGS];    /* end offset of region */
  102. X} *regexp_registers_t;
  103. X
  104. X/* bit definitions for syntax */
  105. X#define RE_NO_BK_PARENS        1    /* no quoting for parentheses */
  106. X#define RE_NO_BK_VBAR        2    /* no quoting for vertical bar */
  107. X#define RE_BK_PLUS_QM        4    /* quoting needed for + and ? */
  108. X#define RE_TIGHT_VBAR        8    /* | binds tighter than ^ and $ */
  109. X#define RE_NEWLINE_OR        16   /* treat newline as or */
  110. X#define RE_CONTEXT_INDEP_OPS    32   /* ^$?*+ are special in all contexts */
  111. X#define RE_ANSI_HEX        64   /* ansi sequences (\n etc) and \xhh */
  112. X#define RE_NO_GNU_EXTENSIONS   128   /* no gnu extensions */
  113. X
  114. X/* definitions for some common regexp styles */
  115. X#define RE_SYNTAX_AWK    (RE_NO_BK_PARENS|RE_NO_BK_VBAR|RE_CONTEXT_INDEP_OPS)
  116. X#define RE_SYNTAX_EGREP    (RE_SYNTAX_AWK|RE_NEWLINE_OR)
  117. X#define RE_SYNTAX_GREP    (RE_BK_PLUS_QM|RE_NEWLINE_OR)
  118. X#define RE_SYNTAX_EMACS    0
  119. X
  120. X#ifdef __STDC__
  121. X
  122. Xint re_set_syntax(int syntax);
  123. X/* This sets the syntax to use and returns the previous syntax.  The
  124. X   syntax is specified by a bit mask of the above defined bits. */
  125. X
  126. Xchar *re_compile_pattern(char *regex, int regex_size, regexp_t compiled);
  127. X/* This compiles the regexp (given in regex and length in regex_size).
  128. X   This returns NULL if the regexp compiled successfully, and an error
  129. X   message if an error was encountered.  The buffer field must be
  130. X   initialized to a memory area allocated by malloc (or to NULL) before
  131. X   use, and the allocated field must be set to its length (or 0 if buffer is
  132. X   NULL).  Also, the translate field must be set to point to a valid
  133. X   translation table, or NULL if it is not used. */
  134. X
  135. Xint re_match(regexp_t compiled, char *string, int size, int pos,
  136. X         regexp_registers_t regs);
  137. X/* This tries to match the regexp against the string.  This returns the
  138. X   length of the matched portion, or -1 if the pattern could not be
  139. X   matched and -2 if an error (such as failure stack overflow) is
  140. X   encountered. */
  141. X
  142. Xint re_match_2(regexp_t compiled, char *string1, int size1,
  143. X          char *string2, int size2, int pos, regexp_registers_t regs,
  144. X           int mstop);
  145. X/* This tries to match the regexp to the concatenation of string1 and
  146. X   string2.  This returns the length of the matched portion, or -1 if the
  147. X   pattern could not be matched and -2 if an error (such as failure stack
  148. X   overflow) is encountered. */
  149. X
  150. Xint re_search(regexp_t compiled, char *string, int size, int startpos,
  151. X          int range, regexp_registers_t regs);
  152. X/* This rearches for a substring matching the regexp.  This returns the first
  153. X   index at which a match is found.  range specifies at how many positions to
  154. X   try matching; positive values indicate searching forwards, and negative
  155. X   values indicate searching backwards.  mstop specifies the offset beyond
  156. X   which a match must not go.  This returns -1 if no match is found, and
  157. X   -2 if an error (such as failure stack overflow) is encountered. */
  158. X
  159. Xint re_search_2(regexp_t compiled, char *string1, int size1,
  160. X        char *string2, int size2, int startpos, int range,
  161. X        regexp_registers_t regs, int mstop);
  162. X/* This is like re_search, but search from the concatenation of string1 and
  163. X   string2.  */
  164. X
  165. Xvoid re_compile_fastmap(regexp_t compiled);
  166. X/* This computes the fastmap for the regexp.  For this to have any effect,
  167. X   the calling program must have initialized the fastmap field to point
  168. X   to an array of 256 characters. */
  169. X
  170. Xchar *re_comp(char *s);
  171. X/* BSD 4.2 regex library routine re_comp.  This compiles the regexp into
  172. X   an internal buffer.  This returns NULL if the regexp was compiled
  173. X   successfully, and an error message if there was an error. */
  174. X
  175. Xint re_exec(char *s);
  176. X/* BSD 4.2 regexp library routine re_exec.  This returns true if the string
  177. X   matches the regular expression (that is, a matching part is found
  178. X   anywhere in the string). */
  179. X
  180. X#else /* __STDC__ */
  181. X
  182. Xint re_set_syntax();
  183. Xchar *re_compile_pattern();
  184. Xint re_match();
  185. Xint re_match_2();
  186. Xint re_search();
  187. Xint re_search_2();
  188. Xvoid re_compile_fastmap();
  189. Xchar *re_comp();
  190. Xint re_exec();
  191. X
  192. X#endif /* __STDC__ */
  193. X
  194. X#endif /* REGEXPR_H */
  195. X
  196. X
  197. END_OF_regexpr.h
  198. if test 5275 -ne `wc -c <regexpr.h`; then
  199.     echo shar: \"regexpr.h\" unpacked with wrong size!
  200. fi
  201. # end of overwriting check
  202. fi
  203. if test -f regexpr.c -a "${1}" != "-c" ; then 
  204.   echo shar: Will not over-write existing file \"regexpr.c\"
  205. else
  206. echo shar: Extracting \"regexpr.c\" \(41626 characters\)
  207. sed "s/^X//" >regexpr.c <<'END_OF_regexpr.c'
  208. X/*
  209. X
  210. Xregexpr.c
  211. X
  212. XAuthor: Tatu Ylonen <ylo@ngs.fi>
  213. X
  214. XCopyright (c) 1991 Tatu Ylonen, Espoo, Finland
  215. X
  216. XPermission to use, copy, modify, distribute, and sell this software
  217. Xand its documentation for any purpose is hereby granted without fee,
  218. Xprovided that the above copyright notice appear in all copies.  This
  219. Xsoftware is provided "as is" without express or implied warranty.
  220. X
  221. XCreated: Thu Sep 26 17:14:05 1991 ylo
  222. XLast modified: Mon Nov  4 17:06:48 1991 ylo
  223. X
  224. XThis code draws many ideas from the regular expression packages by
  225. XHenry Spencer of the University of Toronto and Richard Stallman of the
  226. XFree Software Foundation.
  227. X
  228. XEmacs-specific code and syntax table code is almost directly borrowed
  229. Xfrom GNU regexp.
  230. X
  231. X$Header: /u/src/lib/tools/RCS/regexpr.c,v 1.1 91/12/30 08:53:37 ylo Exp $
  232. X
  233. X*/
  234. X
  235. X#include <stdio.h>
  236. X#include <assert.h>
  237. X#include "regexpr.h"
  238. X
  239. Xchar *malloc();
  240. Xvoid free();
  241. Xchar *realloc();
  242. X
  243. X#define MACRO_BEGIN do {
  244. X#define MACRO_END } while (0)
  245. X
  246. Xenum regexp_compiled_ops /* opcodes for compiled regexp */
  247. X{
  248. X  Cend,            /* end of pattern reached */
  249. X  Cbol,            /* beginning of line */
  250. X  Ceol,            /* end of line */
  251. X  Cset,            /* character set.  Followed by 32 bytes of set. */
  252. X  Cexact,        /* followed by a byte to match */
  253. X  Canychar,        /* matches any character except newline */
  254. X  Cstart_memory,    /* set register start addr (followed by reg number) */
  255. X  Cend_memory,        /* set register end addr (followed by reg number) */
  256. X  Cmatch_memory,    /* match a duplicate of reg contents (regnum follows)*/
  257. X  Cjump,        /* followed by two bytes (lsb,msb) of displacement. */
  258. X  Cstar_jump,        /* will change to jump/update_failure_jump at runtime */
  259. X  Cfailure_jump,    /* jump to addr on failure */
  260. X  Cupdate_failure_jump,    /* update topmost failure point and jump */
  261. X  Cdummy_failure_jump,    /* push a dummy failure point and jump */
  262. X  Cbegbuf,        /* match at beginning of buffer */
  263. X  Cendbuf,        /* match at end of buffer */
  264. X  Cwordbeg,        /* match at beginning of word */
  265. X  Cwordend,        /* match at end of word */
  266. X  Cwordbound,        /* match if at word boundary */
  267. X  Cnotwordbound,    /* match if not at word boundary */
  268. X#ifdef emacs
  269. X  Cemacs_at_dot,    /* emacs only: matches at dot */
  270. X#endif /* emacs */
  271. X  Csyntaxspec,        /* matches syntax code (1 byte follows) */
  272. X  Cnotsyntaxspec    /* matches if syntax code does not match (1 byte foll)*/
  273. X};
  274. X
  275. Xenum regexp_syntax_op    /* syntax codes for plain and quoted characters */
  276. X{
  277. X  Rend,            /* special code for end of regexp */
  278. X  Rnormal,        /* normal character */
  279. X  Ranychar,        /* any character except newline */
  280. X  Rquote,        /* the quote character */
  281. X  Rbol,            /* match beginning of line */
  282. X  Reol,            /* match end of line */
  283. X  Roptional,        /* match preceding expression optionally */
  284. X  Rstar,        /* match preceding expr zero or more times */
  285. X  Rplus,        /* match preceding expr one or more times */
  286. X  Ror,            /* match either of alternatives */
  287. X  Ropenpar,        /* opening parenthesis */
  288. X  Rclosepar,        /* closing parenthesis */
  289. X  Rmemory,        /* match memory register */
  290. X  Rextended_memory,    /* \vnn to match registers 10-99 */
  291. X  Ropenset,        /* open set.  Internal syntax hard-coded below. */
  292. X  /* the following are gnu extensions to "normal" regexp syntax */
  293. X  Rbegbuf,        /* beginning of buffer */
  294. X  Rendbuf,        /* end of buffer */
  295. X  Rwordchar,        /* word character */
  296. X  Rnotwordchar,        /* not word character */
  297. X  Rwordbeg,        /* beginning of word */
  298. X  Rwordend,        /* end of word */
  299. X  Rwordbound,        /* word bound */
  300. X  Rnotwordbound,    /* not word bound */
  301. X#ifdef emacs
  302. X  Remacs_at_dot,    /* emacs: at dot */
  303. X  Remacs_syntaxspec,    /* syntaxspec */
  304. X  Remacs_notsyntaxspec,    /* notsyntaxspec */
  305. X#endif /* emacs */
  306. X  Rnum_ops
  307. X};
  308. X
  309. Xstatic int re_compile_initialized = 0;
  310. Xstatic int regexp_syntax = 0;
  311. Xstatic unsigned char regexp_plain_ops[256];
  312. Xstatic unsigned char regexp_quoted_ops[256];
  313. Xstatic unsigned char regexp_precedences[Rnum_ops];
  314. Xstatic int regexp_context_indep_ops;
  315. Xstatic int regexp_ansi_sequences;
  316. X
  317. X#define NUM_LEVELS  5    /* number of precedence levels in use */
  318. X#define MAX_NESTING 100  /* max nesting level of operators */
  319. X
  320. X#ifdef emacs
  321. X
  322. X/* This code is for emacs compatibility only. */
  323. X
  324. X#include "config.h"
  325. X#include "lisp.h"
  326. X#include "buffer.h"
  327. X#include "syntax.h"
  328. X
  329. X/* emacs defines NULL in some strange way? */
  330. X#undef NULL
  331. X#define NULL 0
  332. X
  333. X#else /* emacs */
  334. X
  335. X#define SYNTAX(ch) re_syntax_table[(unsigned char)(ch)]
  336. X#define Sword 1
  337. X
  338. X#ifdef SYNTAX_TABLE
  339. Xchar *re_syntax_table;
  340. X#else
  341. Xstatic char re_syntax_table[256];
  342. X#endif /* SYNTAX_TABLE */
  343. X
  344. X#endif /* emacs */
  345. X
  346. Xstatic void re_compile_initialize()
  347. X{
  348. X  int a;
  349. X  
  350. X#if !defined(emacs) && !defined(SYNTAX_TABLE)
  351. X  static int syntax_table_inited = 0;
  352. X  
  353. X  if (!syntax_table_inited)
  354. X    {
  355. X      syntax_table_inited = 1;
  356. X      memset(re_syntax_table, 0, 256);
  357. X      for (a = 'a'; a <= 'z'; a++)
  358. X    re_syntax_table[a] = Sword;
  359. X      for (a = 'A'; a <= 'Z'; a++)
  360. X    re_syntax_table[a] = Sword;
  361. X      for (a = '0'; a <= '9'; a++)
  362. X    re_syntax_table[a] = Sword;
  363. X    }
  364. X#endif /* !emacs && !SYNTAX_TABLE */
  365. X  re_compile_initialized = 1;
  366. X  for (a = 0; a < 256; a++)
  367. X    {
  368. X      regexp_plain_ops[a] = Rnormal;
  369. X      regexp_quoted_ops[a] = Rnormal;
  370. X    }
  371. X  for (a = '0'; a <= '9'; a++)
  372. X    regexp_quoted_ops[a] = Rmemory;
  373. X  regexp_plain_ops['\134'] = Rquote;
  374. X  if (regexp_syntax & RE_NO_BK_PARENS)
  375. X    {
  376. X      regexp_plain_ops['('] = Ropenpar;
  377. X      regexp_plain_ops[')'] = Rclosepar;
  378. X    }
  379. X  else
  380. X    {
  381. X      regexp_quoted_ops['('] = Ropenpar;
  382. X      regexp_quoted_ops[')'] = Rclosepar;
  383. X    }
  384. X  if (regexp_syntax & RE_NO_BK_VBAR)
  385. X    regexp_plain_ops['\174'] = Ror;
  386. X  else
  387. X    regexp_quoted_ops['\174'] = Ror;
  388. X  regexp_plain_ops['*'] = Rstar;
  389. X  if (regexp_syntax & RE_BK_PLUS_QM)
  390. X    {
  391. X      regexp_quoted_ops['+'] = Rplus;
  392. X      regexp_quoted_ops['?'] = Roptional;
  393. X    }
  394. X  else
  395. X    {
  396. X      regexp_plain_ops['+'] = Rplus;
  397. X      regexp_plain_ops['?'] = Roptional;
  398. X    }
  399. X  if (regexp_syntax & RE_NEWLINE_OR)
  400. X    regexp_plain_ops['\n'] = Ror;
  401. X  regexp_plain_ops['\133'] = Ropenset;
  402. X  regexp_plain_ops['\136'] = Rbol;
  403. X  regexp_plain_ops['$'] = Reol;
  404. X  regexp_plain_ops['.'] = Ranychar;
  405. X  if (!(regexp_syntax & RE_NO_GNU_EXTENSIONS))
  406. X    {
  407. X#ifdef emacs
  408. X      regexp_quoted_ops['='] = Remacs_at_dot;
  409. X      regexp_quoted_ops['s'] = Remacs_syntaxspec;
  410. X      regexp_quoted_ops['S'] = Remacs_notsyntaxspec;
  411. X#endif /* emacs */
  412. X      regexp_quoted_ops['w'] = Rwordchar;
  413. X      regexp_quoted_ops['W'] = Rnotwordchar;
  414. X      regexp_quoted_ops['<'] = Rwordbeg;
  415. X      regexp_quoted_ops['>'] = Rwordend;
  416. X      regexp_quoted_ops['b'] = Rwordbound;
  417. X      regexp_quoted_ops['B'] = Rnotwordbound;
  418. X      regexp_quoted_ops['`'] = Rbegbuf;
  419. X      regexp_quoted_ops['\''] = Rendbuf;
  420. X    }
  421. X  if (regexp_syntax & RE_ANSI_HEX)
  422. X    regexp_quoted_ops['v'] = Rextended_memory;
  423. X  for (a = 0; a < Rnum_ops; a++)
  424. X    regexp_precedences[a] = 4;
  425. X  if (regexp_syntax & RE_TIGHT_VBAR)
  426. X    {
  427. X      regexp_precedences[Ror] = 3;
  428. X      regexp_precedences[Rbol] = 2;
  429. X      regexp_precedences[Reol] = 2;
  430. X    }
  431. X  else
  432. X    {
  433. X      regexp_precedences[Ror] = 2;
  434. X      regexp_precedences[Rbol] = 3;
  435. X      regexp_precedences[Reol] = 3;
  436. X    }
  437. X  regexp_precedences[Rclosepar] = 1;
  438. X  regexp_precedences[Rend] = 0;
  439. X  regexp_context_indep_ops = (regexp_syntax & RE_CONTEXT_INDEP_OPS) != 0;
  440. X  regexp_ansi_sequences = (regexp_syntax & RE_ANSI_HEX) != 0;
  441. X}
  442. X
  443. Xint re_set_syntax(syntax)
  444. Xint syntax;
  445. X{
  446. X  int ret;
  447. X
  448. X  ret = regexp_syntax;
  449. X  regexp_syntax = syntax;
  450. X  re_compile_initialize();
  451. X  return ret;
  452. X}
  453. X
  454. Xstatic int hex_char_to_decimal(ch)
  455. Xint ch;
  456. X{
  457. X  if (ch >= '0' && ch <= '9')
  458. X    return ch - '0';
  459. X  if (ch >= 'a' && ch <= 'f')
  460. X    return ch - 'a' + 10;
  461. X  if (ch >= 'A' && ch <= 'F')
  462. X    return ch - 'A' + 10;
  463. X  return 16;
  464. X}
  465. X
  466. Xchar *re_compile_pattern(regex, size, bufp)
  467. Xchar *regex;
  468. Xint size;
  469. Xregexp_t bufp;
  470. X{
  471. X  int a, pos, op, current_level, level, opcode;
  472. X  int pattern_offset, alloc;
  473. X  int starts[NUM_LEVELS * MAX_NESTING], starts_base;
  474. X  int future_jumps[MAX_NESTING], num_jumps;
  475. X  unsigned char ch;
  476. X  char *pattern, *translate;
  477. X  int next_register, paren_depth, num_open_registers, open_registers[RE_NREGS];
  478. X  int beginning_context;
  479. X
  480. X#define NEXTCHAR(var)            \
  481. X  MACRO_BEGIN                \
  482. X    if (pos >= size)            \
  483. X      goto ends_prematurely;        \
  484. X    (var) = regex[pos];            \
  485. X    pos++;                \
  486. X  MACRO_END
  487. X
  488. X#define ALLOC(amount)                \
  489. X  MACRO_BEGIN                    \
  490. X    if (pattern_offset+(amount) > alloc)    \
  491. X      {                        \
  492. X    alloc += 256 + (amount);        \
  493. X    pattern = realloc(pattern, alloc);    \
  494. X    if (!pattern)                \
  495. X      goto out_of_memory;            \
  496. X      }                        \
  497. X  MACRO_END
  498. X
  499. X#define STORE(ch) pattern[pattern_offset++] = (ch)
  500. X
  501. X#define CURRENT_LEVEL_START (starts[starts_base + current_level])
  502. X
  503. X#define SET_LEVEL_START starts[starts_base + current_level] = pattern_offset
  504. X
  505. X#define PUSH_LEVEL_STARTS if (starts_base < (MAX_NESTING-1)*NUM_LEVELS) \
  506. X                    starts_base += NUM_LEVELS;            \
  507. X                          else                        \
  508. X                goto too_complex
  509. X
  510. X#define POP_LEVEL_STARTS starts_base -= NUM_LEVELS
  511. X
  512. X#define PUT_ADDR(offset,addr)                \
  513. X  MACRO_BEGIN                        \
  514. X    int disp = (addr) - (offset) - 2;            \
  515. X    pattern[(offset)] = disp & 0xff;            \
  516. X    pattern[(offset)+1] = (disp>>8) & 0xff;        \
  517. X  MACRO_END
  518. X
  519. X#define INSERT_JUMP(pos,type,addr)            \
  520. X  MACRO_BEGIN                        \
  521. X    int a, p = (pos), t = (type), ad = (addr);        \
  522. X    for (a = pattern_offset - 1; a >= p; a--)        \
  523. X      pattern[a + 3] = pattern[a];            \
  524. X    pattern[p] = t;                    \
  525. X    PUT_ADDR(p+1,ad);                    \
  526. X    pattern_offset += 3;                \
  527. X  MACRO_END
  528. X
  529. X#define SETBIT(buf,offset,bit) (buf)[(offset)+(bit)/8] |= (1<<((bit) & 7))
  530. X
  531. X#define SET_FIELDS                \
  532. X  MACRO_BEGIN                    \
  533. X    bufp->allocated = alloc;            \
  534. X    bufp->buffer = pattern;            \
  535. X    bufp->used = pattern_offset;        \
  536. X  MACRO_END
  537. X    
  538. X#define GETHEX(var)                        \
  539. X  MACRO_BEGIN                            \
  540. X    char gethex_ch, gethex_value;                \
  541. X    NEXTCHAR(gethex_ch);                    \
  542. X    gethex_value = hex_char_to_decimal(gethex_ch);        \
  543. X    if (gethex_value == 16)                    \
  544. X      goto hex_error;                        \
  545. X    NEXTCHAR(gethex_ch);                    \
  546. X    gethex_ch = hex_char_to_decimal(gethex_ch);            \
  547. X    if (gethex_ch == 16)                    \
  548. X      goto hex_error;                        \
  549. X    (var) = gethex_value * 16 + gethex_ch;            \
  550. X  MACRO_END
  551. X
  552. X#define ANSI_TRANSLATE(ch)                \
  553. X  MACRO_BEGIN                        \
  554. X    switch (ch)                        \
  555. X      {                            \
  556. X      case 'a':                        \
  557. X      case 'A':                        \
  558. X    ch = 7; /* audible bell */            \
  559. X    break;                        \
  560. X      case 'b':                        \
  561. X      case 'B':                        \
  562. X    ch = 8; /* backspace */                \
  563. X    break;                        \
  564. X      case 'f':                        \
  565. X      case 'F':                        \
  566. X    ch = 12; /* form feed */            \
  567. X    break;                        \
  568. X      case 'n':                        \
  569. X      case 'N':                        \
  570. X    ch = 10; /* line feed */            \
  571. X    break;                        \
  572. X      case 'r':                        \
  573. X      case 'R':                        \
  574. X    ch = 13; /* carriage return */            \
  575. X    break;                        \
  576. X      case 't':                        \
  577. X      case 'T':                        \
  578. X    ch = 9; /* tab */                \
  579. X    break;                        \
  580. X      case 'v':                        \
  581. X      case 'V':                        \
  582. X    ch = 11; /* vertical tab */            \
  583. X    break;                        \
  584. X      case 'x': /* hex code */                \
  585. X      case 'X':                        \
  586. X    GETHEX(ch);                    \
  587. X    break;                        \
  588. X      default:                        \
  589. X    /* other characters passed through */        \
  590. X    if (translate)                    \
  591. X      ch = translate[(unsigned char)ch];        \
  592. X    break;                        \
  593. X      }                            \
  594. X  MACRO_END
  595. X
  596. X  if (!re_compile_initialized)
  597. X    re_compile_initialize();
  598. X  bufp->used = 0;
  599. X  bufp->fastmap_accurate = 0;
  600. X  bufp->uses_registers = 0;
  601. X  translate = bufp->translate;
  602. X  pattern = bufp->buffer;
  603. X  alloc = bufp->allocated;
  604. X  if (alloc == 0 || pattern == NULL)
  605. X    {
  606. X      alloc = 256;
  607. X      pattern = malloc(alloc);
  608. X      if (!pattern)
  609. X    goto out_of_memory;
  610. X    }
  611. X  pattern_offset = 0;
  612. X  starts_base = 0;
  613. X  num_jumps = 0;
  614. X  current_level = 0;
  615. X  SET_LEVEL_START;
  616. X  num_open_registers = 0;
  617. X  next_register = 1;
  618. X  paren_depth = 0;
  619. X  beginning_context = 1;
  620. X  op = -1;
  621. X  /* we use Rend dummy to ensure that pending jumps are updated (due to
  622. X     low priority of Rend) before exiting the loop. */
  623. X  pos = 0;
  624. X  while (op != Rend)
  625. X    {
  626. X      if (pos >= size)
  627. X    op = Rend;
  628. X      else
  629. X    {
  630. X      NEXTCHAR(ch);
  631. X      if (translate)
  632. X        ch = translate[(unsigned char)ch];
  633. X      op = regexp_plain_ops[(unsigned char)ch];
  634. X      if (op == Rquote)
  635. X        {
  636. X          NEXTCHAR(ch);
  637. X          op = regexp_quoted_ops[(unsigned char)ch];
  638. X          if (op == Rnormal && regexp_ansi_sequences)
  639. X        ANSI_TRANSLATE(ch);
  640. X        }
  641. X    }
  642. X      level = regexp_precedences[op];
  643. X      /* printf("ch='%c' op=%d level=%d current_level=%d curlevstart=%d\n",
  644. X         ch, op, level, current_level, CURRENT_LEVEL_START); */
  645. X      if (level > current_level)
  646. X    {
  647. X      for (current_level++; current_level < level; current_level++)
  648. X        SET_LEVEL_START;
  649. X      SET_LEVEL_START;
  650. X    }
  651. X      else
  652. X    if (level < current_level)
  653. X      {
  654. X        current_level = level;
  655. X        for (;num_jumps > 0 &&
  656. X         future_jumps[num_jumps-1] >= CURRENT_LEVEL_START;
  657. X         num_jumps--)
  658. X          PUT_ADDR(future_jumps[num_jumps-1], pattern_offset);
  659. X      }
  660. X      switch (op)
  661. X    {
  662. X    case Rend:
  663. X      break;
  664. X    case Rnormal:
  665. X    normal_char:
  666. X      opcode = Cexact;
  667. X    store_opcode_and_arg: /* opcode & ch must be set */
  668. X      SET_LEVEL_START;
  669. X      ALLOC(2);
  670. X      STORE(opcode);
  671. X      STORE(ch);
  672. X      break;
  673. X    case Ranychar:
  674. X      opcode = Canychar;
  675. X    store_opcode:
  676. X      SET_LEVEL_START;
  677. X      ALLOC(1);
  678. X      STORE(opcode);
  679. X      break;
  680. X    case Rquote:
  681. X      abort();
  682. X      /*NOTREACHED*/
  683. X    case Rbol:
  684. X      if (!beginning_context)
  685. X        if (regexp_context_indep_ops)
  686. X          goto op_error;
  687. X        else
  688. X          goto normal_char;
  689. X      opcode = Cbol;
  690. X      goto store_opcode;
  691. X    case Reol:
  692. X      if (!((pos >= size) ||
  693. X        ((regexp_syntax & RE_NO_BK_VBAR) ?
  694. X         (regex[pos] == '\174') :
  695. X         (pos+1 < size && regex[pos] == '\134' &&
  696. X          regex[pos+1] == '\174')) ||
  697. X        ((regexp_syntax & RE_NO_BK_PARENS)?
  698. X         (regex[pos] == ')'):
  699. X         (pos+1 < size && regex[pos] == '\134' &&
  700. X          regex[pos+1] == ')'))))
  701. X        if (regexp_context_indep_ops)
  702. X          goto op_error;
  703. X        else
  704. X          goto normal_char;
  705. X      opcode = Ceol;
  706. X      goto store_opcode;
  707. X      break;
  708. X    case Roptional:
  709. X      if (beginning_context)
  710. X        if (regexp_context_indep_ops)
  711. X          goto op_error;
  712. X        else
  713. X          goto normal_char;
  714. X      if (CURRENT_LEVEL_START == pattern_offset)
  715. X        break; /* ignore empty patterns for ? */
  716. X      ALLOC(3);
  717. X      INSERT_JUMP(CURRENT_LEVEL_START, Cfailure_jump,
  718. X              pattern_offset + 3);
  719. X      break;
  720. X    case Rstar:
  721. X    case Rplus:
  722. X      if (beginning_context)
  723. X        if (regexp_context_indep_ops)
  724. X          goto op_error;
  725. X        else
  726. X          goto normal_char;
  727. X      if (CURRENT_LEVEL_START == pattern_offset)
  728. X        break; /* ignore empty patterns for + and * */
  729. X      ALLOC(9);
  730. X      INSERT_JUMP(CURRENT_LEVEL_START, Cfailure_jump,
  731. X              pattern_offset + 6);
  732. X      INSERT_JUMP(pattern_offset, Cstar_jump, CURRENT_LEVEL_START);
  733. X      if (op == Rplus)  /* jump over initial failure_jump */
  734. X        INSERT_JUMP(CURRENT_LEVEL_START, Cdummy_failure_jump,
  735. X            CURRENT_LEVEL_START + 6);
  736. X      break;
  737. X    case Ror:
  738. X      ALLOC(6);
  739. X      INSERT_JUMP(CURRENT_LEVEL_START, Cfailure_jump,
  740. X              pattern_offset + 6);
  741. X      if (num_jumps >= MAX_NESTING)
  742. X        goto too_complex;
  743. X      STORE(Cjump);
  744. X      future_jumps[num_jumps++] = pattern_offset;
  745. X      STORE(0);
  746. X      STORE(0);
  747. X      SET_LEVEL_START;
  748. X      break;
  749. X    case Ropenpar:
  750. X      SET_LEVEL_START;
  751. X      if (next_register < RE_NREGS)
  752. X        {
  753. X          bufp->uses_registers = 1;
  754. X          ALLOC(2);
  755. X          STORE(Cstart_memory);
  756. X          STORE(next_register);
  757. X          open_registers[num_open_registers++] = next_register;
  758. X          next_register++;
  759. X        }
  760. X      paren_depth++;
  761. X      PUSH_LEVEL_STARTS;
  762. X      current_level = 0;
  763. X      SET_LEVEL_START;
  764. X      break;
  765. X    case Rclosepar:
  766. X      if (paren_depth <= 0)
  767. X        goto parenthesis_error;
  768. X      POP_LEVEL_STARTS;
  769. X      current_level = regexp_precedences[Ropenpar];
  770. X      paren_depth--;
  771. X      if (paren_depth < num_open_registers)
  772. X        {
  773. X          bufp->uses_registers = 1;
  774. X          ALLOC(2);
  775. X          STORE(Cend_memory);
  776. X          num_open_registers--;
  777. X          STORE(open_registers[num_open_registers]);
  778. X        }
  779. X      break;
  780. X    case Rmemory:
  781. X      if (ch == '0')
  782. X        goto bad_match_register;
  783. X      assert(ch >= '0' && ch <= '9');
  784. X      bufp->uses_registers = 1;
  785. X      opcode = Cmatch_memory;
  786. X      ch -= '0';
  787. X      goto store_opcode_and_arg;
  788. X    case Rextended_memory:
  789. X      NEXTCHAR(ch);
  790. X      if (ch < '0' || ch > '9')
  791. X        goto bad_match_register;
  792. X      NEXTCHAR(a);
  793. X      if (a < '0' || a > '9')
  794. X        goto bad_match_register;
  795. X      ch = 10 * (a - '0') + ch - '0';
  796. X      if (ch <= 0 || ch >= RE_NREGS)
  797. X        goto bad_match_register;
  798. X      bufp->uses_registers = 1;
  799. X      opcode = Cmatch_memory;
  800. X      goto store_opcode_and_arg;
  801. X    case Ropenset:
  802. X      {
  803. X        int complement,prev,offset,range,firstchar;
  804. X        
  805. X        SET_LEVEL_START;
  806. X        ALLOC(1+256/8);
  807. X        STORE(Cset);
  808. X        offset = pattern_offset;
  809. X        for (a = 0; a < 256/8; a++)
  810. X          STORE(0);
  811. X        NEXTCHAR(ch);
  812. X        if (translate)
  813. X          ch = translate[(unsigned char)ch];
  814. X        if (ch == '\136')
  815. X          {
  816. X        complement = 1;
  817. X        NEXTCHAR(ch);
  818. X        if (translate)
  819. X          ch = translate[(unsigned char)ch];
  820. X          }
  821. X        else
  822. X          complement = 0;
  823. X        prev = -1;
  824. X        range = 0;
  825. X        firstchar = 1;
  826. X        while (ch != '\135' || firstchar)
  827. X          {
  828. X        firstchar = 0;
  829. X        if (regexp_ansi_sequences && ch == '\134')
  830. X          {
  831. X            NEXTCHAR(ch);
  832. X            ANSI_TRANSLATE(ch);
  833. X          }
  834. X        if (range)
  835. X          {
  836. X            for (a = prev; a <= ch; a++)
  837. X              SETBIT(pattern, offset, a);
  838. X            prev = -1;
  839. X            range = 0;
  840. X          }
  841. X        else
  842. X          if (prev != -1 && ch == '-')
  843. X            range = 1;
  844. X          else
  845. X            {
  846. X              SETBIT(pattern, offset, ch);
  847. X              prev = ch;
  848. X            }
  849. X        NEXTCHAR(ch);
  850. X        if (translate)
  851. X          ch = translate[(unsigned char)ch];
  852. X          }
  853. X        if (range)
  854. X          SETBIT(pattern, offset, '-');
  855. X        if (complement)
  856. X          {
  857. X        for (a = 0; a < 256/8; a++)
  858. X          pattern[offset+a] ^= 0xff;
  859. X          }
  860. X        break;
  861. X      }
  862. X    case Rbegbuf:
  863. X      opcode = Cbegbuf;
  864. X      goto store_opcode;
  865. X    case Rendbuf:
  866. X      opcode = Cendbuf;
  867. X      goto store_opcode;
  868. X    case Rwordchar:
  869. X      opcode = Csyntaxspec;
  870. X      ch = Sword;
  871. X      goto store_opcode_and_arg;
  872. X    case Rnotwordchar:
  873. X      opcode = Cnotsyntaxspec;
  874. X      ch = Sword;
  875. X      goto store_opcode_and_arg;
  876. X    case Rwordbeg:
  877. X      opcode = Cwordbeg;
  878. X      goto store_opcode;
  879. X    case Rwordend:
  880. X      opcode = Cwordend;
  881. X      goto store_opcode;
  882. X    case Rwordbound:
  883. X      opcode = Cwordbound;
  884. X      goto store_opcode;
  885. X    case Rnotwordbound:
  886. X      opcode = Cnotwordbound;
  887. X      goto store_opcode;
  888. X#ifdef emacs
  889. X    case Remacs_at_dot:
  890. X      opcode = Cemacs_at_dot;
  891. X      goto store_opcode;
  892. X    case Remacs_syntaxspec:
  893. X      NEXTCHAR(ch);
  894. X      if (translate)
  895. X        ch = translate[(unsigned char)ch];
  896. X      opcode = Csyntaxspec;
  897. X      ch = syntax_spec_code[(unsigned char)ch];
  898. X      goto store_opcode_and_arg;
  899. X    case Remacs_notsyntaxspec:
  900. X      NEXTCHAR(ch);
  901. X      if (translate)
  902. X        ch = translate[(unsigned char)ch];
  903. X      opcode = Cnotsyntaxspec;
  904. X      ch = syntax_spec_code[(unsigned char)ch];
  905. X      goto store_opcode_and_arg;
  906. X#endif /* emacs */
  907. X    default:
  908. X      abort();
  909. X    }
  910. X      beginning_context = (op == Ropenpar || op == Ror);
  911. X    }
  912. X  if (starts_base != 0)
  913. X    goto parenthesis_error;
  914. X  assert(num_jumps == 0);
  915. X  ALLOC(1);
  916. X  STORE(Cend);
  917. X  SET_FIELDS;
  918. X  return NULL;
  919. X
  920. X op_error:
  921. X  SET_FIELDS;
  922. X  return "Badly placed special character";
  923. X
  924. X bad_match_register:
  925. X  SET_FIELDS;
  926. X  return "Bad match register number";
  927. X
  928. X hex_error:
  929. X  SET_FIELDS;
  930. X  return "Bad hexadecimal number";
  931. X
  932. X parenthesis_error:
  933. X  SET_FIELDS;
  934. X  return "Badly placed parenthesis";
  935. X
  936. X out_of_memory:
  937. X  SET_FIELDS;
  938. X  return "Out of memory";
  939. X
  940. X ends_prematurely:
  941. X  SET_FIELDS;
  942. X  return "Regular expression ends prematurely";
  943. X
  944. X too_complex:
  945. X  SET_FIELDS;
  946. X  return "Regular expression too complex";
  947. X}
  948. X#undef CHARAT
  949. X#undef NEXTCHAR
  950. X#undef GETHEX
  951. X#undef ALLOC
  952. X#undef STORE
  953. X#undef CURRENT_LEVEL_START
  954. X#undef SET_LEVEL_START
  955. X#undef PUSH_LEVEL_STARTS
  956. X#undef POP_LEVEL_STARTS
  957. X#undef PUT_ADDR
  958. X#undef INSERT_JUMP
  959. X#undef SETBIT
  960. X#undef SET_FIELDS
  961. X
  962. Xstatic void re_compile_fastmap_aux(code, pos, visited, can_be_null, fastmap)
  963. Xchar *code, *visited, *can_be_null, *fastmap;
  964. Xint pos;
  965. X{
  966. X  int a, b, syntaxcode;
  967. X
  968. X  if (visited[pos])
  969. X    return;  /* we have already been here */
  970. X  visited[pos] = 1;
  971. X  for (;;)
  972. X    switch (code[pos++])
  973. X      {
  974. X      case Cend:
  975. X    *can_be_null = 1;
  976. X    return;
  977. X      case Cbol:
  978. X      case Cbegbuf:
  979. X      case Cendbuf:
  980. X      case Cwordbeg:
  981. X      case Cwordend:
  982. X      case Cwordbound:
  983. X      case Cnotwordbound:
  984. X#ifdef emacs
  985. X      case Cemacs_at_dot:
  986. X#endif /* emacs */
  987. X    break;
  988. X      case Csyntaxspec:
  989. X    syntaxcode = code[pos++];
  990. X    for (a = 0; a < 256; a++)
  991. X      if (SYNTAX(a) == syntaxcode)
  992. X        fastmap[a] = 1;
  993. X    return;
  994. X      case Cnotsyntaxspec:
  995. X    syntaxcode = code[pos++];
  996. X    for (a = 0; a < 256; a++)
  997. X      if (SYNTAX(a) != syntaxcode)
  998. X        fastmap[a] = 1;
  999. X    return;
  1000. X      case Ceol:
  1001. X    fastmap['\n'] = 1;
  1002. X    if (*can_be_null == 0)
  1003. X      *can_be_null = 2;  /* can match null, but only at end of buffer*/
  1004. X    return;
  1005. X      case Cset:
  1006. X    for (a = 0; a < 256/8; a++)
  1007. X      if (code[pos + a] != 0)
  1008. X        for (b = 0; b < 8; b++)
  1009. X          if (code[pos + a] & (1 << b))
  1010. X        fastmap[(a << 3) + b] = 1;
  1011. X    pos += 256/8;
  1012. X    return;
  1013. X      case Cexact:
  1014. X    fastmap[(unsigned char)code[pos]] = 1;
  1015. X    return;
  1016. X      case Canychar:
  1017. X    for (a = 0; a < 256; a++)
  1018. X      if (a != '\n')
  1019. X        fastmap[a] = 1;
  1020. X    return;
  1021. X      case Cstart_memory:
  1022. X      case Cend_memory:
  1023. X    pos++;
  1024. X    break;
  1025. X      case Cmatch_memory:
  1026. X    /* should this ever happen for sensible patterns??? */
  1027. X    *can_be_null = 1;
  1028. X    return;
  1029. X      case Cjump:
  1030. X      case Cdummy_failure_jump:
  1031. X      case Cupdate_failure_jump:
  1032. X      case Cstar_jump:
  1033. X    a = (unsigned char)code[pos++];
  1034. X    a |= (unsigned char)code[pos++] << 8;
  1035. X    pos += (int)(short)a;
  1036. X    if (visited[pos])
  1037. X      {
  1038. X        /* argh... the regexp contains empty loops.  This is not
  1039. X           good, as this may cause a failure stack overflow when
  1040. X           matching.  Oh well. */
  1041. X        /* this path leads nowhere; pursue other paths. */
  1042. X        return;
  1043. X      }
  1044. X    visited[pos] = 1;
  1045. X    break;
  1046. X      case Cfailure_jump:
  1047. X    a = (unsigned char)code[pos++];
  1048. X    a |= (unsigned char)code[pos++] << 8;
  1049. X    a = pos + (int)(short)a;
  1050. X    re_compile_fastmap_aux(code, a, visited, can_be_null, fastmap);
  1051. X    break;
  1052. X      default:
  1053. X    abort();  /* probably some opcode is missing from this switch */
  1054. X    /*NOTREACHED*/
  1055. X      }
  1056. X}
  1057. X
  1058. Xstatic int re_do_compile_fastmap(buffer, used, pos, can_be_null, fastmap)
  1059. Xchar *buffer, *fastmap, *can_be_null;
  1060. Xint used, pos;
  1061. X{
  1062. X  char small_visited[512], *visited;
  1063. X
  1064. X  if (used <= sizeof(small_visited))
  1065. X    visited = small_visited;
  1066. X  else
  1067. X    {
  1068. X      visited = malloc(used);
  1069. X      if (!visited)
  1070. X    return 0;
  1071. X    }
  1072. X  *can_be_null = 0;
  1073. X  memset(fastmap, 0, 256);
  1074. X  memset(visited, 0, used);
  1075. X  re_compile_fastmap_aux(buffer, pos, visited, can_be_null, fastmap);
  1076. X  if (visited != small_visited)
  1077. X    free(visited);
  1078. X  return 1;
  1079. X}
  1080. X
  1081. Xvoid re_compile_fastmap(bufp)
  1082. Xregexp_t bufp;
  1083. X{
  1084. X  if (!bufp->fastmap || bufp->fastmap_accurate)
  1085. X    return;
  1086. X  assert(bufp->used > 0);
  1087. X  if (!re_do_compile_fastmap(bufp->buffer, bufp->used, 0, &bufp->can_be_null,
  1088. X                 bufp->fastmap))
  1089. X    return;
  1090. X  if (bufp->buffer[0] == Cbol)
  1091. X    bufp->anchor = 1;   /* begline */
  1092. X  else
  1093. X    if (bufp->buffer[0] == Cbegbuf)
  1094. X      bufp->anchor = 2; /* begbuf */
  1095. X    else
  1096. X      bufp->anchor = 0; /* none */
  1097. X  bufp->fastmap_accurate = 1;
  1098. X}
  1099. X
  1100. X#define INITIAL_FAILURES  128  /* initial # failure points to allocate */
  1101. X#define MAX_FAILURES     4100  /* max # of failure points before failing */
  1102. X
  1103. Xint re_match_2(bufp, string1, size1, string2, size2, pos, regs, mstop)
  1104. Xregexp_t bufp;
  1105. Xchar *string1, *string2;
  1106. Xint size1, size2, pos, mstop;
  1107. Xregexp_registers_t regs;
  1108. X{
  1109. X  struct failure_point { char *text, *partend, *code; }
  1110. X    *failure_stack_start, *failure_sp, *failure_stack_end,
  1111. X    initial_failure_stack[INITIAL_FAILURES];
  1112. X  char *code, *translate, *text, *textend, *partend, *part_2_end;
  1113. X  char *regstart_text[RE_NREGS], *regstart_partend[RE_NREGS];
  1114. X  char *regend_text[RE_NREGS], *regend_partend[RE_NREGS];
  1115. X  int a, b, ch, reg, regch, match_end;
  1116. X  char *regtext, *regpartend, *regtextend;
  1117. X
  1118. X#define PREFETCH                    \
  1119. X  MACRO_BEGIN                        \
  1120. X    if (text == partend)                \
  1121. X      {                            \
  1122. X    if (text == textend)                \
  1123. X      goto fail;                    \
  1124. X    text = string2;                    \
  1125. X    partend = part_2_end;                \
  1126. X      }                            \
  1127. X  MACRO_END
  1128. X
  1129. X#define NEXTCHAR(var)                \
  1130. X  MACRO_BEGIN                    \
  1131. X    PREFETCH;                    \
  1132. X    (var) = (unsigned char)*text++;        \
  1133. X    if (translate)                \
  1134. X      (var) = (unsigned char)translate[(var)];    \
  1135. X  MACRO_END
  1136. X
  1137. X  assert(pos >= 0 && size1 >= 0 && size2 >= 0 && mstop >= 0);
  1138. X  assert(mstop <= size1 + size2);
  1139. X  assert(pos <= mstop);
  1140. X
  1141. X  if (pos <= size1)
  1142. X    {
  1143. X      text = string1 + pos;
  1144. X      if (mstop <= size1)
  1145. X    {
  1146. X      partend = string1 + mstop;
  1147. X      textend = partend;
  1148. X    }
  1149. X      else
  1150. X    {
  1151. X      partend = string1 + size1;
  1152. X      textend = string2 + mstop - size1;
  1153. X    }
  1154. X      part_2_end = string2 + mstop - size1;
  1155. X    }
  1156. X  else
  1157. X    {
  1158. X      text = string2 + pos - size1;
  1159. X      partend = string2 + mstop - size1;
  1160. X      textend = partend;
  1161. X      part_2_end = partend;
  1162. X    }
  1163. X
  1164. X  if (bufp->uses_registers && regs != NULL)
  1165. X    for (a = 0; a < RE_NREGS; a++)
  1166. X      regend_text[a] = NULL;
  1167. X
  1168. X  code = bufp->buffer;
  1169. X  translate = bufp->translate;
  1170. X  failure_stack_start = failure_sp = initial_failure_stack;
  1171. X  failure_stack_end = initial_failure_stack + INITIAL_FAILURES;
  1172. X
  1173. X#if 0
  1174. X  /* re_search_2 has already done this, and otherwise we get little benefit
  1175. X     from this.  So I'll leave this out. */
  1176. X  if (bufp->fastmap_accurate && !bufp->can_be_null &&
  1177. X      text != textend &&
  1178. X      !bufp->fastmap[translate ?
  1179. X             (unsigned char)translate[(unsigned char)*text] :
  1180. X             (unsigned char)*text])
  1181. X    return -1;  /* it can't possibly match */
  1182. X#endif
  1183. X
  1184. X continue_matching:
  1185. X  for (;;)
  1186. X    {
  1187. X      switch (*code++)
  1188. X    {
  1189. X    case Cend:
  1190. X      if (partend != part_2_end)
  1191. X        match_end = text - string1;
  1192. X      else
  1193. X        match_end = text - string2 + size1;
  1194. X      if (regs)
  1195. X        {
  1196. X          regs->start[0] = pos;
  1197. X          regs->end[0] = match_end;
  1198. X          if (!bufp->uses_registers)
  1199. X        {
  1200. X          for (a = 1; a < RE_NREGS; a++)
  1201. X            {
  1202. X              regs->start[a] = -1;
  1203. X              regs->end[a] = -1;
  1204. X            }
  1205. X        }
  1206. X          else
  1207. X        {
  1208. X          for (a = 1; a < RE_NREGS; a++)
  1209. X            {
  1210. X              if (regend_text[a] == NULL)
  1211. X            {
  1212. X              regs->start[a] = -1;
  1213. X              regs->end[a] = -1;
  1214. X              continue;
  1215. X            }
  1216. X              if (regstart_partend[a] != part_2_end)
  1217. X            regs->start[a] = regstart_text[a] - string1;
  1218. X              else
  1219. X            regs->start[a] = regstart_text[a] - string2 + size1;
  1220. X              if (regend_partend[a] != part_2_end)
  1221. X            regs->end[a] = regend_text[a] - string1;
  1222. X              else
  1223. X            regs->end[a] = regend_text[a] - string2 + size1;
  1224. X            }
  1225. X        }
  1226. X        }
  1227. X      if (failure_stack_start != initial_failure_stack)
  1228. X        free((char *)failure_stack_start);
  1229. X      return match_end - pos;
  1230. X    case Cbol:
  1231. X      if (text == string1 || text[-1] == '\n') /* text[-1] always valid */
  1232. X        break;
  1233. X      goto fail;
  1234. X    case Ceol:
  1235. X      if (text == string2 + size2 ||
  1236. X          (text == string1 + size1 ?
  1237. X           (size2 == 0 || *string2 == '\n') :
  1238. X           *text == '\n'))
  1239. X        break;
  1240. X      goto fail;
  1241. X    case Cset:
  1242. X      NEXTCHAR(ch);
  1243. X      if (code[ch/8] & (1<<(ch & 7)))
  1244. X        {
  1245. X          code += 256/8;
  1246. X          break;
  1247. X        }
  1248. X      goto fail;
  1249. X    case Cexact:
  1250. X      NEXTCHAR(ch);
  1251. X      if (ch != (unsigned char)*code++)
  1252. X        goto fail;
  1253. X      break;
  1254. X    case Canychar:
  1255. X      NEXTCHAR(ch);
  1256. X      if (ch == '\n')
  1257. X        goto fail;
  1258. X      break;
  1259. X    case Cstart_memory:
  1260. X      reg = *code++;
  1261. X      regstart_text[reg] = text;
  1262. X      regstart_partend[reg] = partend;
  1263. X      break;
  1264. X    case Cend_memory:
  1265. X      reg = *code++;
  1266. X      regend_text[reg] = text;
  1267. X      regend_partend[reg] = partend;
  1268. X      break;
  1269. X    case Cmatch_memory:
  1270. X      reg = *code++;
  1271. X      if (regend_text[reg] == NULL)
  1272. X        goto fail;  /* or should we just match nothing? */
  1273. X      regtext = regstart_text[reg];
  1274. X      regtextend = regend_text[reg];
  1275. X      if (regstart_partend[reg] == regend_partend[reg])
  1276. X        regpartend = regtextend;
  1277. X      else
  1278. X        regpartend = string1 + size1;
  1279. X      
  1280. X      for (;regtext != regtextend;)
  1281. X        {
  1282. X          NEXTCHAR(ch);
  1283. X          if (regtext == regpartend)
  1284. X        regtext = string2;
  1285. X          regch = (unsigned char)*regtext++;
  1286. X          if (translate)
  1287. X        regch = (unsigned char)translate[regch];
  1288. X          if (regch != ch)
  1289. X        goto fail;
  1290. X        }
  1291. X      break;
  1292. X    case Cstar_jump:
  1293. X      /* star is coded as:
  1294. X           1: failure_jump 2
  1295. X              ... code for operand of star
  1296. X          star_jump 1
  1297. X           2: ... code after star
  1298. X         We change the star_jump to update_failure_jump if we can determine
  1299. X         that it is safe to do so; otherwise we change it to an ordinary
  1300. X         jump.
  1301. X         plus is coded as
  1302. X              jump 2
  1303. X           1: failure_jump 3
  1304. X           2: ... code for operand of plus
  1305. X              star_jump 1
  1306. X           3: ... code after plus
  1307. X         For star_jump considerations this is processed identically
  1308. X         to star. */
  1309. X      a = (unsigned char)*code++;
  1310. X      a |= (unsigned char)*code++ << 8;
  1311. X      a = (int)(short)a;
  1312. X      {
  1313. X        char map[256], can_be_null;
  1314. X        char *p1, *p2;
  1315. X
  1316. X        p1 = code + a + 3; /* skip the failure_jump */
  1317. X        assert(p1[-3] == Cfailure_jump);
  1318. X        p2 = code;
  1319. X        /* p1 points inside loop, p2 points to after loop */
  1320. X        if (!re_do_compile_fastmap(bufp->buffer, bufp->used,
  1321. X                       p2 - bufp->buffer, &can_be_null, map))
  1322. X          goto make_normal_jump;
  1323. X        /* If we might introduce a new update point inside the loop,
  1324. X           we can't optimize because then update_jump would update a
  1325. X           wrong failure point.  Thus we have to be quite careful here. */
  1326. X      loop_p1:
  1327. X        /* loop until we find something that consumes a character */
  1328. X        switch (*p1++)
  1329. X          {
  1330. X              case Cbol:
  1331. X              case Ceol:
  1332. X              case Cbegbuf:
  1333. X              case Cendbuf:
  1334. X              case Cwordbeg:
  1335. X              case Cwordend:
  1336. X              case Cwordbound:
  1337. X              case Cnotwordbound:
  1338. X#ifdef emacs
  1339. X              case Cemacs_at_dot:
  1340. X#endif /* emacs */
  1341. X                goto loop_p1;
  1342. X              case Cstart_memory:
  1343. X              case Cend_memory:
  1344. X                p1++;
  1345. X                goto loop_p1;
  1346. X          case Cexact:
  1347. X        ch = (unsigned char)*p1++;
  1348. X        if (map[ch])
  1349. X          goto make_normal_jump;
  1350. X        break;
  1351. X          case Canychar:
  1352. X        for (b = 0; b < 256; b++)
  1353. X          if (b != '\n' && map[b])
  1354. X            goto make_normal_jump;
  1355. X        break;
  1356. X          case Cset:
  1357. X        for (b = 0; b < 256; b++)
  1358. X          if ((p1[b >> 3] & (1 << (b & 7))) && map[b])
  1359. X            goto make_normal_jump;
  1360. X        p1 += 256/8;
  1361. X        break;
  1362. X          default:
  1363. X        goto make_normal_jump;
  1364. X          }
  1365. X        /* now we know that we can't backtrack. */
  1366. X        while (p1 != p2 - 3)
  1367. X          {
  1368. X        switch (*p1++)
  1369. X          {
  1370. X          case Cend:
  1371. X            abort();  /* we certainly shouldn't get this inside loop */
  1372. X            /*NOTREACHED*/
  1373. X          case Cbol:
  1374. X          case Ceol:
  1375. X          case Canychar:
  1376. X          case Cbegbuf:
  1377. X          case Cendbuf:
  1378. X          case Cwordbeg:
  1379. X          case Cwordend:
  1380. X          case Cwordbound:
  1381. X          case Cnotwordbound:
  1382. X#ifdef emacs
  1383. X          case Cemacs_at_dot:
  1384. X#endif /* emacs */
  1385. X            break;
  1386. X          case Cset:
  1387. X            p1 += 256/8;
  1388. X            break;
  1389. X          case Cexact:
  1390. X          case Cstart_memory:
  1391. X          case Cend_memory:
  1392. X          case Cmatch_memory:
  1393. X          case Csyntaxspec:
  1394. X          case Cnotsyntaxspec:
  1395. X            p1++;
  1396. X            break;
  1397. X          case Cjump:
  1398. X          case Cstar_jump:
  1399. X          case Cfailure_jump:
  1400. X          case Cupdate_failure_jump:
  1401. X          case Cdummy_failure_jump:
  1402. X            goto make_normal_jump;
  1403. X          default:
  1404. X            printf("regexpr.c: processing star_jump: unknown op %d\n", p1[-1]);
  1405. X            break;
  1406. X          }
  1407. X          }
  1408. X        goto make_update_jump;
  1409. X      }
  1410. X    make_normal_jump:
  1411. X      /* printf("changing to normal jump\n"); */
  1412. X      code -= 3;
  1413. X      *code = Cjump;
  1414. X      break;
  1415. X    make_update_jump:
  1416. X      /* printf("changing to update jump\n"); */
  1417. X      code -= 2;
  1418. X      a += 3;  /* jump to after the Cfailure_jump */
  1419. X      code[-1] = Cupdate_failure_jump;
  1420. X      code[0] = a & 0xff;
  1421. X      code[1] = a >> 8;
  1422. X      /* fall to next case */
  1423. X    case Cupdate_failure_jump:
  1424. X      failure_sp[-1].text = text;
  1425. X      failure_sp[-1].partend = partend;
  1426. X      /* fall to next case */
  1427. X    case Cjump:
  1428. X      a = (unsigned char)*code++;
  1429. X      a |= (unsigned char)*code++ << 8;
  1430. X      code += (int)(short)a;
  1431. X      break;
  1432. X    case Cdummy_failure_jump:
  1433. X    case Cfailure_jump:
  1434. X      if (failure_sp == failure_stack_end)
  1435. X        {
  1436. X          if (failure_stack_start != initial_failure_stack)
  1437. X        goto error;
  1438. X          failure_stack_start = (struct failure_point *)
  1439. X        malloc(MAX_FAILURES * sizeof(*failure_stack_start));
  1440. X          failure_stack_end = failure_stack_start + MAX_FAILURES;
  1441. X          memcpy((char *)failure_stack_start, (char *)initial_failure_stack,
  1442. X             INITIAL_FAILURES * sizeof(*failure_stack_start));
  1443. X          failure_sp = failure_stack_start + INITIAL_FAILURES;
  1444. X        }
  1445. X      a = (unsigned char)*code++;
  1446. X      a |= (unsigned char)*code++ << 8;
  1447. X      a = (int)(short)a;
  1448. X      if (code[-3] == Cdummy_failure_jump)
  1449. X        { /* this is only used in plus */
  1450. X          assert(*code == Cfailure_jump);
  1451. X          b = (unsigned char)code[1];
  1452. X          b |= (unsigned char)code[2] << 8;
  1453. X          failure_sp->code = code + (int)(short)b + 3;
  1454. X          failure_sp->text = NULL;
  1455. X          code += a;
  1456. X        }
  1457. X      else
  1458. X        {
  1459. X          failure_sp->code = code + a;
  1460. X          failure_sp->text = text;
  1461. X          failure_sp->partend = partend;
  1462. X        }
  1463. X      failure_sp++;
  1464. X      break;
  1465. X    case Cbegbuf:
  1466. X      if (text == string1)
  1467. X        break;
  1468. X      goto fail;
  1469. X    case Cendbuf:
  1470. X      if (size2 == 0 ? text == string1 + size1 : text == string2 + size2)
  1471. X        break;
  1472. X      goto fail;
  1473. X    case Cwordbeg:
  1474. X      if (text == string2 + size2)
  1475. X        goto fail;
  1476. X      if (size2 == 0 && text == string1 + size1)
  1477. X        goto fail;
  1478. X      if (SYNTAX(text == string1 + size1 ? *string1 : *text) != Sword)
  1479. X        goto fail;
  1480. X      if (text == string1)
  1481. X        break;
  1482. X      if (SYNTAX(text[-1]) != Sword)
  1483. X        break;
  1484. X      goto fail;
  1485. X    case Cwordend:
  1486. X      if (text == string1)
  1487. X        goto fail;
  1488. X      if (SYNTAX(text[-1]) != Sword)
  1489. X        goto fail;
  1490. X      if (text == string2 + size2)
  1491. X        break;
  1492. X      if (size2 == 0 && text == string1 + size1)
  1493. X        break;
  1494. X      if (SYNTAX(*text) == Sword)
  1495. X        goto fail;
  1496. X      break;
  1497. X    case Cwordbound:
  1498. X      /* Note: as in gnu regexp, this also matches at the beginning
  1499. X         and end of buffer. */
  1500. X      if (text == string1 || text == string2 + size2 ||
  1501. X          (size2 == 0 && text == string1 + size1))
  1502. X        break;
  1503. X      if ((SYNTAX(text[-1]) == Sword) ^
  1504. X          (SYNTAX(text == string1 + size1 ? *string2 : *text) == Sword))
  1505. X        break;
  1506. X      goto fail;
  1507. X    case Cnotwordbound:
  1508. X      /* Note: as in gnu regexp, this never matches at the beginning
  1509. X         and end of buffer. */
  1510. X      if (text == string1 || text == string2 + size2 ||
  1511. X          (size2 == 0 && text == string1 + size1))
  1512. X        goto fail;
  1513. X      if (!((SYNTAX(text[-1]) == Sword) ^
  1514. X        (SYNTAX(text == string1 + size1 ? *string2 : *text) == Sword)))
  1515. X        goto fail;
  1516. X      break;
  1517. X    case Csyntaxspec:
  1518. X      NEXTCHAR(ch);
  1519. X      if (SYNTAX(ch) != (unsigned char)*code++)
  1520. X        goto fail;
  1521. X      break;
  1522. X    case Cnotsyntaxspec:
  1523. X      NEXTCHAR(ch);
  1524. X      if (SYNTAX(ch) != (unsigned char)*code++)
  1525. X        break;
  1526. X      goto fail;
  1527. X#ifdef emacs
  1528. X    case Cemacs_at_dot:
  1529. X      if (PTR_CHAR_POS((unsigned char *)text) + 1 != point)
  1530. X        goto fail;
  1531. X      break;
  1532. X#endif /* emacs */
  1533. X    default:
  1534. X      abort();
  1535. X      /*NOTREACHED*/
  1536. X    }
  1537. X    }
  1538. X  abort();
  1539. X  /*NOTREACHED*/
  1540. X
  1541. X fail:
  1542. X  if (failure_sp != failure_stack_start)
  1543. X    {
  1544. X      failure_sp--;
  1545. X      text = failure_sp->text;
  1546. X      if (text == NULL)
  1547. X    goto fail;
  1548. X      partend = failure_sp->partend;
  1549. X      code = failure_sp->code;
  1550. X      goto continue_matching;
  1551. X    }
  1552. X  if (failure_stack_start != initial_failure_stack)
  1553. X    free((char *)failure_stack_start);
  1554. X  return -1;
  1555. X
  1556. X error:
  1557. X  if (failure_stack_start != initial_failure_stack)
  1558. X    free((char *)failure_stack_start);
  1559. X  return -2;
  1560. X}
  1561. X
  1562. X#undef PREFETCH
  1563. X#undef NEXTCHAR
  1564. X#undef PUSH_FAILURE
  1565. X
  1566. Xint re_match(bufp, string, size, pos, regs)
  1567. Xregexp_t bufp;
  1568. Xchar *string;
  1569. Xint size, pos;
  1570. Xregexp_registers_t regs;
  1571. X{
  1572. X  return re_match_2(bufp, string, size, (char *)NULL, 0, pos, regs, size);
  1573. X}
  1574. X
  1575. Xint re_search_2(bufp, string1, size1, string2, size2, pos, range, regs,
  1576. X        mstop)
  1577. Xregexp_t bufp;
  1578. Xchar *string1, *string2;
  1579. Xint size1, size2, pos, range, mstop;
  1580. Xregexp_registers_t regs;
  1581. X{
  1582. X  char *fastmap, *translate, *text, *partstart, *partend;
  1583. X  int dir, ret;
  1584. X  char anchor;
  1585. X  
  1586. X  assert(size1 >= 0 && size2 >= 0 && pos >= 0 && mstop >= 0);
  1587. X  assert(pos + range + 1 >= 0 && pos + range - 1 <= size1 + size2);
  1588. X  assert(pos <= mstop);
  1589. X  
  1590. X  fastmap = bufp->fastmap;
  1591. X  translate = bufp->translate;
  1592. X  if (fastmap && !bufp->fastmap_accurate)
  1593. X    re_compile_fastmap(bufp);
  1594. X  anchor = bufp->anchor;
  1595. X  if (bufp->can_be_null == 1) /* can_be_null == 2: can match null at eob */
  1596. X    fastmap = NULL;
  1597. X  if (range < 0)
  1598. X    {
  1599. X      dir = -1;
  1600. X      range = -range;
  1601. X    }
  1602. X  else
  1603. X    dir = 1;
  1604. X  if (anchor == 2)
  1605. X    if (pos != 0)
  1606. X      return -1;
  1607. X    else
  1608. X      range = 0;
  1609. X  for (; range >= 0; range--, pos += dir)
  1610. X    {
  1611. X      if (fastmap)
  1612. X    {
  1613. X      if (dir == 1)
  1614. X        { /* searching forwards */
  1615. X          if (pos < size1)
  1616. X        {
  1617. X          text = string1 + pos;
  1618. X          if (pos + range > size1)
  1619. X            partend = string1 + size1;
  1620. X          else
  1621. X            partend = string1 + pos + range;
  1622. X        }
  1623. X          else
  1624. X        {
  1625. X          text = string2 + pos - size1;
  1626. X          partend = string2 + pos + range - size1;
  1627. X        }
  1628. X          partstart = text;
  1629. X          if (translate)
  1630. X        while (text != partend &&
  1631. X               !fastmap[(unsigned char)
  1632. X                translate[(unsigned char)*text]])
  1633. X          text++;
  1634. X          else
  1635. X        while (text != partend && !fastmap[(unsigned char)*text])
  1636. X          text++;
  1637. X          pos += text - partstart;
  1638. X          range -= text - partstart;
  1639. X          if (pos == size1 + size2 && bufp->can_be_null == 0)
  1640. X        return -1;
  1641. X        }
  1642. X      else
  1643. X        { /* searching backwards */
  1644. X          if (pos <= size1)
  1645. X        {
  1646. X          text = string1 + pos;
  1647. X          partstart = string1 + pos - range;
  1648. X        }
  1649. X          else
  1650. X        {
  1651. X          text = string2 + pos - size1;
  1652. X          if (range < pos - size1)
  1653. X            partstart = string2 + pos - size1 - range;
  1654. X          else
  1655. X            partstart = string2;
  1656. X        }
  1657. X          partend = text;
  1658. X          if (translate)
  1659. X        while (text != partstart &&
  1660. X               !fastmap[(unsigned char)
  1661. X                translate[(unsigned char)*text]])
  1662. X          text--;
  1663. X          else
  1664. X        while (text != partstart &&
  1665. X               !fastmap[(unsigned char)*text])
  1666. X          text--;
  1667. X          pos -= partend - text;
  1668. X          range -= partend - text;
  1669. X        }
  1670. X    }
  1671. X      if (anchor == 1)
  1672. X    { /* anchored to begline */
  1673. X      if (pos > 0 &&
  1674. X          (pos <= size1 ? string1[pos - 1] :
  1675. X           string2[pos - size1 - 1]) != '\n')
  1676. X        continue;
  1677. X    }
  1678. X      assert(pos >= 0 && pos <= size1 + size2);
  1679. X      ret = re_match_2(bufp, string1, size1, string2, size2, pos, regs, mstop);
  1680. X      if (ret >= 0)
  1681. X    return pos;
  1682. X      if (ret == -2)
  1683. X    return -2;
  1684. X    }
  1685. X  return -1;
  1686. X}
  1687. X
  1688. Xint re_search(bufp, string, size, startpos, range, regs)
  1689. Xregexp_t bufp;
  1690. Xchar *string;
  1691. Xint size, startpos, range;
  1692. Xregexp_registers_t regs;
  1693. X{
  1694. X  return re_search_2(bufp, string, size, (char *)NULL, 0,
  1695. X             startpos, range, regs, size);
  1696. X}
  1697. X
  1698. Xstatic struct re_pattern_buffer re_comp_buf;
  1699. X
  1700. Xchar *re_comp(s)
  1701. Xchar *s;
  1702. X{
  1703. X  if (s == NULL)
  1704. X    {
  1705. X      if (!re_comp_buf.buffer)
  1706. X    return "Out of memory";
  1707. X      return NULL;
  1708. X    }
  1709. X  if (!re_comp_buf.buffer)
  1710. X    {
  1711. X      /* the buffer will be allocated automatically */
  1712. X      re_comp_buf.fastmap = malloc(256);
  1713. X      re_comp_buf.translate = NULL;
  1714. X    }
  1715. X  return re_compile_pattern(s, strlen(s), &re_comp_buf);
  1716. X}
  1717. X
  1718. Xint re_exec(s)
  1719. Xchar *s;
  1720. X{
  1721. X  int len = strlen(s);
  1722. X  
  1723. X  return re_search(&re_comp_buf, s, len, 0, len, (regexp_registers_t)NULL) >= 0;
  1724. X}
  1725. X
  1726. X#ifdef TEST_REGEXP
  1727. X
  1728. Xint main()
  1729. X{
  1730. X  char buf[500];
  1731. X  char *cp;
  1732. X  struct re_pattern_buffer exp;
  1733. X  struct re_registers regs;
  1734. X  int a,pos;
  1735. X  char fastmap[256];
  1736. X
  1737. X  exp.allocated = 0;
  1738. X  exp.buffer = 0;
  1739. X  exp.translate = NULL;
  1740. X  exp.fastmap = fastmap;
  1741. X
  1742. X  /* re_set_syntax(RE_NO_BK_PARENS|RE_NO_BK_VBAR|RE_ANSI_HEX); */
  1743. X
  1744. X  while (1)
  1745. X    {
  1746. X      printf("Enter regexp:\n");
  1747. X      gets(buf);
  1748. X      cp=re_compile_pattern(buf, strlen(buf), &exp);
  1749. X      if (cp)
  1750. X    {
  1751. X      printf("Error: %s\n", cp);
  1752. X      continue;
  1753. X    }
  1754. X      re_compile_fastmap(&exp);
  1755. X      printf("dump:\n");
  1756. X      for (pos = 0; pos < exp.used;)
  1757. X    {
  1758. X      printf("%d: ", pos);
  1759. X      switch (exp.buffer[pos++])
  1760. X        {
  1761. X        case Cend:
  1762. X          strcpy(buf, "end");
  1763. X          break;
  1764. X        case Cbol:
  1765. X          strcpy(buf, "bol");
  1766. X          break;
  1767. X        case Ceol:
  1768. X          strcpy(buf, "eol");
  1769. X          break;
  1770. X        case Cset:
  1771. X          strcpy(buf, "set ");
  1772. X          for (a = 0; a < 256/8; a++)
  1773. X        sprintf(buf+strlen(buf)," %02x",
  1774. X            (unsigned char)exp.buffer[pos++]);
  1775. X          break;
  1776. X        case Cexact:
  1777. X          sprintf(buf, "exact '%c' 0x%x", exp.buffer[pos],
  1778. X              (unsigned char)exp.buffer[pos]);
  1779. X          pos++;
  1780. X          break;
  1781. X        case Canychar:
  1782. X          strcpy(buf, "anychar");
  1783. X          break;
  1784. X        case Cstart_memory:
  1785. X          sprintf(buf, "start_memory %d", exp.buffer[pos++]);
  1786. X          break;
  1787. X        case Cend_memory:
  1788. X          sprintf(buf, "end_memory %d", exp.buffer[pos++]);
  1789. X          break;
  1790. X        case Cmatch_memory:
  1791. X          sprintf(buf, "match_memory %d", exp.buffer[pos++]);
  1792. X          break;
  1793. X        case Cjump:
  1794. X        case Cdummy_failure_jump:
  1795. X        case Cstar_jump:
  1796. X        case Cfailure_jump:
  1797. X        case Cupdate_failure_jump:
  1798. X          a = (unsigned char)exp.buffer[pos++];
  1799. X          a += (unsigned char)exp.buffer[pos++] << 8;
  1800. X          a = (int)(short)a;
  1801. X          switch (exp.buffer[pos-3])
  1802. X        {
  1803. X        case Cjump:
  1804. X          cp = "jump";
  1805. X          break;
  1806. X        case Cstar_jump:
  1807. X          cp = "star_jump";
  1808. X          break;
  1809. X        case Cfailure_jump:
  1810. X          cp = "failure_jump";
  1811. X          break;
  1812. X        case Cupdate_failure_jump:
  1813. X          cp = "update_failure_jump";
  1814. X          break;
  1815. X        case Cdummy_failure_jump:
  1816. X          cp = "dummy_failure_jump";
  1817. X          break;
  1818. X        default:
  1819. X          cp = "unknown jump";
  1820. X          break;
  1821. X        }
  1822. X          sprintf(buf, "%s %d", cp, a + pos);
  1823. X          break;
  1824. X        case Cbegbuf:
  1825. X          strcpy(buf,"begbuf");
  1826. X          break;
  1827. X        case Cendbuf:
  1828. X          strcpy(buf,"endbuf");
  1829. X          break;
  1830. X        case Cwordbeg:
  1831. X          strcpy(buf,"wordbeg");
  1832. X          break;
  1833. X        case Cwordend:
  1834. X          strcpy(buf,"wordend");
  1835. X          break;
  1836. X        case Cwordbound:
  1837. X          strcpy(buf,"wordbound");
  1838. X          break;
  1839. X        case Cnotwordbound:
  1840. X          strcpy(buf,"notwordbound");
  1841. X          break;
  1842. X        default:
  1843. X          sprintf(buf, "unknown code %d",
  1844. X              (unsigned char)exp.buffer[pos - 1]);
  1845. X          break;
  1846. X        }
  1847. X      printf("%s\n", buf);
  1848. X    }
  1849. X      printf("can_be_null = %d uses_registers = %d anchor = %d\n",
  1850. X         exp.can_be_null, exp.uses_registers, exp.anchor);
  1851. X      
  1852. X      printf("fastmap:");
  1853. X      for (a = 0; a < 256; a++)
  1854. X    if (exp.fastmap[a])
  1855. X      printf(" %d", a);
  1856. X      printf("\n");
  1857. X      printf("Enter strings.  An empty line terminates.\n");
  1858. X      while (fgets(buf, sizeof(buf), stdin))
  1859. X    {
  1860. X      if (buf[0] == '\n')
  1861. X        break;
  1862. X      a = re_search(&exp, buf, strlen(buf), 0, strlen(buf), ®s);
  1863. X      printf("search returns %d\n", a);
  1864. X      if (a != -1)
  1865. X        {
  1866. X          for (a = 0; a < RE_NREGS; a++)
  1867. X        {
  1868. X          printf("buf %d: %d to %d\n", a, regs.start[a], regs.end[a]);
  1869. X        }
  1870. X        }
  1871. X    }
  1872. X    }
  1873. X}
  1874. X
  1875. X#endif /* TEST_REGEXP */
  1876. END_OF_regexpr.c
  1877. if test 41626 -ne `wc -c <regexpr.c`; then
  1878.     echo shar: \"regexpr.c\" unpacked with wrong size!
  1879. fi
  1880. # end of overwriting check
  1881. fi
  1882. echo shar: End of shell archive.
  1883. exit 0
  1884.  
  1885. exit 0 # Just in case...
  1886. -- 
  1887. Kent Landfield                   INTERNET: kent@sparky.IMD.Sterling.COM
  1888. Sterling Software, IMD           UUCP:     uunet!sparky!kent
  1889. Phone:    (402) 291-8300         FAX:      (402) 291-4362
  1890. Please send comp.sources.misc-related mail to kent@uunet.uu.net.
  1891.